/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModuloControladorBD;

import ModuloBaseDelConocimiento.SintomaOSigno;
import ModuloControladorBD.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ModuloBaseDelConocimiento.Caso;

/**
 *
 * @author rayner
 */
public class SintomaOSignoJpaController {

    public SintomaOSignoJpaController() {
        emf = Persistence.createEntityManagerFactory("medicalDiagnosisRBC_ANNPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SintomaOSigno sintomaOSigno) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caso caso = sintomaOSigno.getCaso();
            if (caso != null) {
                caso = em.getReference(caso.getClass(), caso.getId());
                sintomaOSigno.setCaso(caso);
            }
            em.persist(sintomaOSigno);
            if (caso != null) {
                caso.getSintomaOSignoList().add(sintomaOSigno);
                caso = em.merge(caso);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SintomaOSigno sintomaOSigno) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SintomaOSigno persistentSintomaOSigno = em.find(SintomaOSigno.class, sintomaOSigno.getId());
            Caso casoOld = persistentSintomaOSigno.getCaso();
            Caso casoNew = sintomaOSigno.getCaso();
            if (casoNew != null) {
                casoNew = em.getReference(casoNew.getClass(), casoNew.getId());
                sintomaOSigno.setCaso(casoNew);
            }
            sintomaOSigno = em.merge(sintomaOSigno);
            if (casoOld != null && !casoOld.equals(casoNew)) {
                casoOld.getSintomaOSignoList().remove(sintomaOSigno);
                casoOld = em.merge(casoOld);
            }
            if (casoNew != null && !casoNew.equals(casoOld)) {
                casoNew.getSintomaOSignoList().add(sintomaOSigno);
                casoNew = em.merge(casoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = sintomaOSigno.getId();
                if (findSintomaOSigno(id) == null) {
                    throw new NonexistentEntityException("The sintomaOSigno with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SintomaOSigno sintomaOSigno;
            try {
                sintomaOSigno = em.getReference(SintomaOSigno.class, id);
                sintomaOSigno.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The sintomaOSigno with id " + id + " no longer exists.", enfe);
            }
            Caso caso = sintomaOSigno.getCaso();
            if (caso != null) {
                caso.getSintomaOSignoList().remove(sintomaOSigno);
                caso = em.merge(caso);
            }
            em.remove(sintomaOSigno);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SintomaOSigno> findSintomaOSignoEntities() {
        return findSintomaOSignoEntities(true, -1, -1);
    }

    public List<SintomaOSigno> findSintomaOSignoEntities(int maxResults, int firstResult) {
        return findSintomaOSignoEntities(false, maxResults, firstResult);
    }

    private List<SintomaOSigno> findSintomaOSignoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(SintomaOSigno.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public SintomaOSigno findSintomaOSigno(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(SintomaOSigno.class, id);
        } finally {
            em.close();
        }
    }

    public int getSintomaOSignoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<SintomaOSigno> rt = cq.from(SintomaOSigno.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
